package com.bbm.communitysystem.controller.post;


import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.core.oss.OSS;
import com.bbm.communitysystem.domain.Comment;
import com.bbm.communitysystem.domain.OssFile;
import com.bbm.communitysystem.domain.bo.CommentBo;
import com.bbm.communitysystem.domain.dto.post.CommentDto;
import com.bbm.communitysystem.service.impl.CommentServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  评论 前端控制器
 * </p>
 *
 * @author transitory
 * @since 2025-02-20
 */
@RestController
@Tag(name = "评论")
@RequiredArgsConstructor
@RequestMapping("/comment")
public class CommentController {

    private final CommentServiceImpl commentService;

    private final OSS oss;


    /**
     * 增加评论 API
     **/
    @Operation(summary = "增加评论")
    @PostMapping
    @Parameters({
            @Parameter(name = "postId", description = "帖子id"),
            @Parameter(name = "content", description = "评论内容"),
            @Parameter(name = "userId", description = "用户id"),
            @Parameter(name = "step", description = "步骤id")

    })
    public R<String> addComment(@RequestParam("postId") Integer id, @RequestParam("content") String content,
                                @RequestParam("userId") Integer userId, @RequestParam("img")MultipartFile file,
                                @RequestParam("step") Integer stepId) {

        CommentBo commentBo = new CommentBo();

        if (file.getSize() != 0) {
            List<OssFile> ossFiles = oss.saveFile("comment", userId + "", file);
            commentBo.setOssId(ossFiles.get(0).getId());
        } else {
            commentBo.setOssId(null);
        }


        commentBo.setLike(0);
        commentBo.setPostId(id);
        commentBo.setContent(content);
        commentBo.setUserId(userId);
        commentBo.setComment(0);
        commentBo.setStepId(stepId);

        commentBo.setStatus(1);

        CommentDto commentDto = commentService.addComment(commentBo);

        return commentDto != null ? R.ok("评论成功") : R.error("评论失败");
    }

    /**
     * 删除评论 API
     * */
    @Operation(summary = "删除评论")
    @DeleteMapping("/{id}")
    @Parameter(name = "id", description = "评论id")
    public R<String> deleteComment(@PathVariable("id") Integer id){

        Comment byId = commentService.getById(id);
        CommentBo commentBo = new CommentBo();
        commentBo.setId(id);
        commentBo.setPostId(byId.getPostId());

        int i = commentService.delComment(commentBo);


        return i == 0 ? R.ok("删除成功") : R.error("删除失败");
    }


}
